/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManageBean;

import UserBN.Authentication;
import UserBN.UserService;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mr-Quick
 */
@ManagedBean
@RequestScoped
public class Loginform {

    String account;
    entity.SwhUser user;

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    String password;

    public Loginform() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
        HttpSession session = request.getSession(true);
        Authentication.getInstance().setSession(session);
    }

    public void login() {
    }

    public entity.SwhUser getUser() {
        if (UserBN.Authentication.getInstance().getUsername() != null) {
            UserService accountSvc = new UserService();
            user = accountSvc.findAccountByUsername(UserBN.Authentication.getInstance().getUsername());
        }
        return user;
    }

    public String checkLogin() {
        try {
            Authentication auth = Authentication.getInstance();
            UserService us = new UserService();
            String r = auth.login(account, password);
            if ("true".equals(r)) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Login Success", "TSuccess");
                FacesContext.getCurrentInstance().addMessage(null, message);
                return "index.xhtml";
                
            }else{
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Login False", "This Message had in your message");
            FacesContext.getCurrentInstance().addMessage(null, message); 
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Login False", "This Message had in your message");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
        return null;
    }

    public void logout() {
        Authentication auth = Authentication.getInstance();
        auth.logout();

    }
}
